package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 8051
 * http://www.bokerb.com/ 博客日报
 * @author KK
 *
 */
public class BokerbBlogAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://[a-z0-9]*.bokerb.com/[0-9]*.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		Integer deliverNum = null;
		String updateUrl = url;
		String author=null;
		Document doc = Jsoup.parse(htmltxt);

		title = doc.select("span.blog_list_title").text();

		date = DateUtils.matchDate(doc.select("td.blog_view_2").text()) ;

		content = HtmlCleaner.getContentHtml(url,  doc.select("div.blog_view_3"));
		author = doc.select("td.blog_table_t1").text();
		clickNum =NumberUtils.matchNumber(doc.select("span:containsOwn(阅读\\()").text());
		commnetNum =NumberUtils.matchNumber(doc.select("a:containsOwn(评论\\()").text());
		deliverNum = NumberUtils.matchNumber(doc.select("a:containsOwn(收藏\\()").text());

			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(7);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(commnetNum);
			news.setClickNum(clickNum);
			news.setAuthor(author);
			//news.setDeliverNum(deliverNum);
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(updateUrl);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta!=null &&StringUtils.isNotBlank(meta.getUpdateUrl())){
			UrlMeta urlMeta = CrawlHTML.responseToURL(meta.getUpdateUrl());
			if (urlMeta!=null && urlMeta.getHtml()!=null){
				return parserHtml(urlMeta);
			}
		}
		return null;
	}

	

	
}
